package org.example.carsystem.service.impl;

import org.example.carsystem.domain.SysDept;
import org.example.carsystem.domain.vo.DeptVo;
import org.example.carsystem.service.DeptService;
import org.example.carsystem.mapper.DeptMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.*;
import java.util.stream.Collectors;

/**
* @author fatso
* @description 针对表【sys_dept(部门表)】的数据库操作Service实现
* @createDate 2024-06-19 21:01:06
*/
@Service
public class DeptServiceImpl implements DeptService {

    @Autowired
    DeptMapper deptMapper;
    @Override
    public List<SysDept> findAll(String deptName, String status) {
        List<SysDept> deptList = deptMapper.findAll();
        List<SysDept> deptParentList = deptList.stream().filter(ss -> ss.getParentId() == 0).collect(Collectors.toList());
        List<SysDept> deptTree = new ArrayList<>();
        //组成树形结构
        for (SysDept dept : deptParentList) {
            deptTree.add(recursion(deptList,dept));
        }
        //单走一个deptName查询
        if (!StringUtils.isEmpty(deptName) && StringUtils.isEmpty(status)){
            System.err.println("name");
            treeSearch(deptTree,deptName);
        }else if (!StringUtils.isEmpty(status) && StringUtils.isEmpty(deptName)){ //单走一个status查询
            System.err.println("status");
            treeSearch(deptTree,status);
        }else if (!StringUtils.isEmpty(deptName) && !StringUtils.isEmpty(status)){ //deptName和status都有
            System.err.println("两个查询条件");
            treeSearch(deptTree,deptName,status);
        }
        return deptTree;
    }

   @Override
    public Map<String, Object> deptSelectAll() {
        List<SysDept> list = deptMapper.findAll();
        List<DeptVo> deptParentList = list.stream().map(ss->new DeptVo(ss.getDeptId(),ss.getParentId(),ss.getDeptName(),null)).collect(Collectors.toList());
       List<DeptVo> deptVoList = deptParentList.stream().filter(ss -> ss.getParentId() == 0).collect(Collectors.toList());
       List<DeptVo> deptTree = new ArrayList<>();
        for (DeptVo deptVo : deptVoList) {
            deptTree.add(recursion(deptParentList,deptVo));
        }
        Map<String, Object> map = new HashMap<>();
        map.put("data",deptTree);
        return map;
    }


    private void treeSearch(List<SysDept> deptTree, String deptName, String status) {
        Iterator<SysDept> iterator = deptTree.iterator();
        while (iterator.hasNext()){
            SysDept dept = iterator.next();
            //同时满足deptName和status的直接返回，否则查询子集是否有符合条件的数据
            if ((!dept.getDeptName().contains(deptName)&&!dept.getStatus().equals(status))||
                (!dept.getDeptName().contains(deptName)&&dept.getStatus().equals(status))||
                (dept.getDeptName().contains(deptName)&&!dept.getStatus().equals(status))){
                List<SysDept> deptVoList = dept.getChildren();
                if(CollectionUtils.isEmpty(deptVoList)){
                    iterator.remove();
                }else {
                    treeSearch(deptVoList,deptName,status);
                    if (CollectionUtils.isEmpty(dept.getChildren())){
                        iterator.remove();
                    }
                }
            }
        }
    }

    private void treeSearch(List<SysDept> deptTree, String param){
        Iterator<SysDept> iterator = deptTree.iterator();
        while (iterator.hasNext()){
            SysDept dept = iterator.next();
            //status查询
            if (param.equals("0")||param.equals("1")){
                if (!dept.getStatus().equals(param)){
                    List<SysDept> deptVoList = dept.getChildren();
                    if(CollectionUtils.isEmpty(deptVoList)){
                        iterator.remove();
                    }else {  //deptName查询
                        treeSearch(deptVoList,param);
                        if (CollectionUtils.isEmpty(dept.getChildren())){
                            iterator.remove();
                        }
                    }
                }
            }else {
                if ((!dept.getDeptName().contains(param))){
                    List<SysDept> deptVoList = dept.getChildren();
                    if(CollectionUtils.isEmpty(deptVoList)){
                        iterator.remove();
                    }else {
                        treeSearch(deptVoList,param);
                        if (CollectionUtils.isEmpty(dept.getChildren())){
                            iterator.remove();
                        }
                    }
                }
            }
        }
    }

    private DeptVo recursion(List<DeptVo> list, DeptVo dept) {
        List<DeptVo> DeptList = new ArrayList<>();
        for (DeptVo deptVo : list) {
            if (null!=deptVo.getParentId() && deptVo.getParentId().equals(dept.getDeptId())){
                DeptList.add(recursion(list,deptVo));
            }
        }
        if (!DeptList.isEmpty()){
            dept.setChildren(DeptList);
        }
        return dept;
    }

    private SysDept recursion(List<SysDept> list, SysDept dept) {
        List<SysDept> DeptList = new ArrayList<>();
        for (SysDept sysDept : list) {
            if (null!= sysDept.getParentId() && sysDept.getParentId().equals(dept.getDeptId())){
                DeptList.add(recursion(list, sysDept));
            }
        }
        if (!DeptList.isEmpty()){
            dept.setChildren(DeptList);
        }
        return dept;
    }


    @Override
    public Integer sysUploud(SysDept sysDept) {
        return deptMapper.sysUploud(sysDept);
    }

    @Override
    public Integer sysDelete(Long deptId) {
        return deptMapper.sysDelete(deptId);
    }

    @Override
    public void sysinstr(SysDept sysDept) {
        deptMapper.sysinstr(sysDept);
    }

    @Override
    public void batchInsert(List<SysDept> list) {
        deptMapper.batchInsert(list);
    }

}




